package com.caizi.base.utils;

import java.util.Map;

/**
 * @Author 菜菜籽
 * @Date 2024-12-06 14:54
 * @description:
 */
public class MapUtils {
    public static String toString(Map<String, Boolean> data) {
        if (data == null) {
            return "";
        }

        StringBuilder sb = new StringBuilder();
        sb.append("{");

        boolean first = true;
        for (Map.Entry<String, Boolean> entry : data.entrySet()) {
            if (!first) {
                sb.append(",");
            } else {
                first = false;
            }

            // 确保键是字符串并用双引号包裹
            sb.append("\"").append(escape(entry.getKey())).append("\":");

            // 直接处理布尔值
            sb.append(entry.getValue());
        }

        sb.append("}");
        return sb.toString();
    }

    // 用于转义JSON字符串中的特殊字符
    private static String escape(String str) {
        return str.replace("\\", "\\\\")
                .replace("\"", "\\\"")
                .replace("\b", "\\b")
                .replace("\t", "\\t")
                .replace("\n", "\\n")
                .replace("\f", "\\f")
                .replace("\r", "\\r");
    }
}
